package com.itheima.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.Port;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public List<Permission> findAllPermission() {
        return permissionDao.findAllPermission();
    }

    /**
     * 分页加条件查询权限
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        if (currentPage != 0 && page.size() == 0){
            currentPage = currentPage - 1;
            PageHelper.startPage(currentPage, pageSize);
            page = permissionDao.selectByCondition(queryString);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新建权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public Permission query(Integer id) {
        Permission query= permissionDao.query(id);
        return query;
    }

    @Override
    public void updateById(Permission permission) {
        permissionDao.updateById(permission);

    }

    @Override
    public boolean deleteById(Integer id) {
        //查询该检查项是否被其他检查组引用
        Integer integer = permissionDao.queryById(id);


        if(integer!=0){
            //其他检查组中 有 该检查项
            return false;
        }else {
            //其他检查组中 没有 该检查项
            permissionDao.deleteById(id);
            return true;
        }

    }
}
